﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ApplicationLibrary.SqlConnector;
using Clothes.Items.ModuleData;
using ApplicationLibrary;
using Clothes.Masters.ModuleData;
using ApplicationLibrary.ExtendedControls;

namespace Clothes.Items.Libraries
{
    public partial class ItemSearcher : UserControl
    {
        EnumsManager<ItemStatus> eItemStatusManager = new EnumsManager<ItemStatus>(Properties.Resources.ResourceManager);
        DataSet dsGrid = null;

        public ItemSearcher()
        {
            InitializeComponent();
            dsGrid = new DataSet("Result");
        }

        public int ItemID {
            get 
            {
                if (dgwResults.CurrentRow != null)
                {
                    return DataGridViewRowValues.GetValue<int>(dgwResults.CurrentRow, CL_Items.ID);
                }
                return -1;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            AttachData();
            base.OnLoad(e);
        }

        private void AttachData()
        {
            dgwResults.AutoGenerateColumns = false;
            BindColumn(rsc_Item, CL_Items.ID);
            BindColumn(rsc_Desc, CL_Items.Description);
            BindColumn(rsc_Price, CL_Items.Price);
        }

        private void BindColumn(DataGridViewColumn gridcol, iColumn column)
        {
            gridcol.Name = column.Name;
            gridcol.DataPropertyName = column.Name;
            gridcol.Tag = column.DefaultValue;
            if (gridcol.CellType == typeof(DataGridViewTextBoxCell) && column.Len > 0)
                ((DataGridViewTextBoxColumn)gridcol).MaxInputLength = column.Len;
        }

        private void ItemSearcher_Load(object sender, EventArgs e)
        {
            if (DesignMode)
                return;

            cbbDesigner.AttachDataReader<Int32>(new RRDesctiptionType(DescriptionType.E_Designer), CL_Descriptions.ID, CL_Descriptions.Description, true);
            cbbSize.AttachDataReader<Int32>(new RRDesctiptionType(DescriptionType.E_Size), CL_Descriptions.ID, CL_Descriptions.Description, true);
            cbbColor.AttachDataReader<Int32>(new RRDesctiptionType(DescriptionType.E_Color), CL_Descriptions.ID, CL_Descriptions.Description, true);
            cbbPrint.AttachDataReader<Int32>(new RRDesctiptionType(DescriptionType.E_Pattern), CL_Descriptions.ID, CL_Descriptions.Description, true);
            cbbMaterial.AttachDataReader<Int32>(new RRDesctiptionType(DescriptionType.E_Material), CL_Descriptions.ID, CL_Descriptions.Description, true);

            rdrCustomer.RadarForm = new Clothes.Masters.ModuleData.RadarMasters();
            rdrCustomer.AttachCounterType(Counters.Masters.Int(), GlobalInfo.CurrentDate, null);
            rdrCustomer.DescriptionControl = lblDesc;
        }

        private void txtDescription_Validating(object sender, CancelEventArgs e)
        {
            ckbDescription.Checked = !txtDescription.Text.IsEmpty();
        }

        private void ckbDesigner_FilterChanged(object sender, EventArgs e)
        {
            EnableCheckBox(sender);
            Requery();
        }

        private void rdrCustomer_Validating(object sender, CancelEventArgs e)
        {
            EnableCheckBox(sender);
            Requery();
        }

        private void EnableCheckBox(object sender)
        {
            Control ctrl = sender as Control;

            switch(ctrl.Name)
            {
                case "cbbDesigner":
                    ckbDesigner.Checked = true;
                    break;
                case "cbbSize":
                    ckbSize.Checked = true;
                    break;
                case "cbbColor":
                    ckbColor.Checked = true;
                    break;
                case "cbbPrint":
                    ckbPrint.Checked = true;
                    break;
                case "cbbMaterial":
                    ckbMaterial.Checked = true;
                    break;
                case "rdrCustomer":
                    ckbCustomer.Checked = true;
                    break;
                case "bdcCateg":
                    ckbCateg.Checked = true;
                    break;
            }
        }

        private void Requery()
        {
            SqlABCommand sqlCM = new SqlABCommand();
            SqlABDataAdapter sqlDA = new SqlABDataAdapter(sqlCM);
            
            sqlCM.Connection = GlobalInfo.SqlConnection;

            QueryBuilder qb = new QueryBuilder();
            qb.AddSelectAll(CL_Items.Name);
            qb.AddFrom(CL_Items.Name);

            if (ckbDesigner.Checked)
            {
                SqlABParameter pDes = new SqlABParameter("@pdes", CL_Items.Designer);
                pDes.Value = cbbDesigner.GetValue<int>();
                sqlCM.Parameters.Add(pDes);
                qb.AddCompare(CL_Items.Designer, pDes);
            }

            if (ckbSize.Checked)
            {
                SqlABParameter pSiz = new SqlABParameter("@pSiz", CL_Items.Size);
                pSiz.Value = cbbSize.GetValue<int>();
                sqlCM.Parameters.Add(pSiz);
                qb.AddCompare(CL_Items.Size, pSiz);
            }

            if (ckbColor.Checked)
            {
                SqlABParameter pCol = new SqlABParameter("@pCol", CL_Items.Color);
                pCol.Value = cbbColor.GetValue<int>();
                sqlCM.Parameters.Add(pCol);
                qb.AddCompare(CL_Items.Color, pCol);
            }

            if (ckbPrint.Checked)
            {
                SqlABParameter pPrt = new SqlABParameter("@pPrt", CL_Items.Pattern);
                pPrt.Value = cbbPrint.GetValue<int>();
                sqlCM.Parameters.Add(pPrt);
                qb.AddCompare(CL_Items.Pattern, pPrt);
            }

            if (ckbMaterial.Checked)
            {
                SqlABParameter pMat = new SqlABParameter("@pMat", CL_Items.Material);
                pMat.Value = cbbMaterial.GetValue<int>();
                sqlCM.Parameters.Add(pMat);
                qb.AddCompare(CL_Items.Material, pMat);
            }

            if (ckbCustomer.Checked)
            {
                SqlABParameter pCus = new SqlABParameter("@pCus", CL_Items.Customer);
                pCus.Value = rdrCustomer.Text;
                sqlCM.Parameters.Add(pCus);
                qb.AddCompare(CL_Items.Customer, pCus);
            }

            if (ckbCateg.Checked)
            {
                SqlABParameter pCat = new SqlABParameter("@pCat", CL_Items.Category);
                pCat.Value = bdcCateg.Text+"%";
                sqlCM.Parameters.Add(pCat);
                qb.AddLike(CL_Items.Category, pCat);
            }

            if (ckbDescription.Checked)
            {
                SqlABParameter pDes = new SqlABParameter("@pDes", CL_Items.Description);
                pDes.Value = string.Concat("%", txtDescription.Text, "%");
                qb.AddLike(CL_Items.Description, pDes);
                sqlCM.Parameters.Add(pDes);
            }

            //SqlABParameter pStatusLoad = new SqlABParameter("@pStaL", CL_Items.Status);
            //SqlABParameter pStatusNull = new SqlABParameter("@pStaN", CL_Items.Status);
            //pStatusLoad.Value = ;
            //pStatusLoad.Value = ;
            qb.AddCompareIN<int>(CL_Items.Status, "{0},{1}", ItemStatus.E_Load.Int(), ItemStatus.E_Null.Int());
            //sqlCM.Parameters.Add(pStatus);

            dsGrid.Clear();
            try
            {
                sqlCM.CommandText = qb.Query;
                sqlDA.Fill(dsGrid, "ResultTable");
                dgwResults.DataView.Table = dsGrid.Tables["ResultTable"];
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.Message);
            }
        }

        private void tsbRefresh_Click(object sender, EventArgs e)
        {
            Requery();
        }

        private void bdcCateg_DescriptionChanged(object sender, EventArgs e)
        {

        }

    }
}
